package com.dog.basics.service.common.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dog.basics.bean.dto.api.common.ApiNewsReadAddDto;
import com.dog.basics.bean.model.common.DogNewsRead;
import com.dog.basics.dao.common.DogNewsReadMapper;
import com.dog.basics.security.util.SecurityUtils;
import com.dog.basics.service.common.IDogNewsReadService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 消息通知用户读取状态 服务实现类
 * </p>
 *
 * @author guonh
 * @since 2020-05-14
 */
@Service
public class DogNewsReadServiceImpl extends ServiceImpl<DogNewsReadMapper, DogNewsRead> implements IDogNewsReadService {

    /**
     * @Description: 增加已读消息
     * @Param: obj
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/05/22  0027
     */
    @Override
    @Transactional
    public Boolean saveObj(ApiNewsReadAddDto obj) {
        List<DogNewsRead> list = this.list(Wrappers.<DogNewsRead>lambdaQuery().eq(DogNewsRead::getUserId, SecurityUtils.getUser().getUserId()).eq(DogNewsRead::getNewsId, obj.getNewsId()));
        if (CollUtil.isNotEmpty(list)) {
            return Boolean.TRUE;
        } else {
            DogNewsRead dogNewsRead = new DogNewsRead();
            BeanUtil.copyProperties(obj, dogNewsRead);
            dogNewsRead.setUserId(SecurityUtils.getUser().getUserId());
            dogNewsRead.setCreateTime(DateUtil.currentSeconds());
            dogNewsRead.setCreateBy(SecurityUtils.getUser().getUserId());
            return this.save(dogNewsRead);
        }

    }
}
